<template>
	<div class="home">
		<el-row style="width: 100%;height: 100%;">
			<el-col :span="16" class="left-box">
			</el-col>
			<el-col :span="8" class="right-box">
				<h1 class="content-title"> 航 空 售 票 管 理 系 统</h1>
				<h4 class="content-title">用 户 注 册</h4>
				<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
					<el-form-item label="姓名" prop="username">
						<el-input v-model="ruleForm.username"></el-input>
					</el-form-item>
					<el-form-item label="性别" prop="sex">
						<el-select v-model="ruleForm.sex" clearable placeholder="请选择" style="width: 100%;">
							<el-option v-for="item in sexEnum" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="身份证号" prop="cardId">
						<el-input v-model="ruleForm.cardId"></el-input>
					</el-form-item>
					<el-form-item label="联系电话" prop="phone">
						<el-input v-model="ruleForm.phone"></el-input>
					</el-form-item>
					<el-form-item label="账号" prop="account">
						<el-input v-model="ruleForm.account"></el-input>
					</el-form-item>
					<el-form-item label="密码" prop="password">
						<el-input v-model="ruleForm.password"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
						<el-button @click="resetForm('ruleForm')">重置</el-button>
					</el-form-item>
				</el-form>
				<el-link type="primary" href="/#/">已有账号？前往登录</el-link>
			</el-col>
		</el-row>
	</div>
</template>

<script>
	const validateCardId = (rule, value, callback) => {
		if (value) {
			const rege =
				/^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/
			if (!rege.test(value)) {
				callback(new Error('请输入正确的身份证号码'))
			} else {
				callback()
			}
		} else {
			callback(new Error('请输入身份证号码'))
		}
	}

	const validatePhone = (rule, value, callback) => {
		if (value) {
			const rege =
				/^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/
			if (!rege.test(value)) {
				callback(new Error('请输入正确的手机号码'))
			} else {
				callback()
			}
		} else {
			callback(new Error('请输入手机号码'))
		}
	}
	export default {
		name: 'Register',
		data() {
			return {
				msg: 'Welcome to Your Vue.js App',
				ruleForm: {
					username: '',
					account: '',
					password: '',
					cardId: '',
					phone: '',
					sex: '',
					auth: 1
				},
				sexEnum: [{
					value: '女',
					label: '女'
				}, {
					value: '男',
					label: '男'
				}],
				rules: {
					account: [{
							required: true,
							message: '请输入账号',
							trigger: 'blur'
						},
						{
							min: 6,
							max: 10,
							message: '长度在 6 到 10 个字符',
							trigger: 'blur'
						}
					],
					sex: {
						required: true,
						message: "请选择性别",
						trigger: "change"
					},
					password: [{
							required: true,
							message: '请输入密码',
							trigger: 'blur'
						},
						{
							min: 8,
							max: 16,
							message: '密码长度在 8 到 16 个字符',
							trigger: 'blur'
						}
					],
					username: [{
							required: true,
							message: '请输入用户名',
							trigger: 'blur'
						},
						{
							min: 2,
							max: 5,
							message: '长度在 2 到 5 个字符',
							trigger: 'blur'
						}
					],
					phone: [{
						required: true,
						validator: validatePhone,
						trigger: 'blur'
					}],
					cardId: [{
						required: true,
						validator: validateCardId,
						trigger: 'blur'
					}],
				}
			}
		},
		methods: {
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.$axios.post("http://localhost:8888/register", this.ruleForm).then((data) => {
							if (data.data == 1) {
								this.$message({
									message: '注册成功',
									type: 'success'
								});
							}
						})
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		}
	}
</script>

<style scoped>
	@import url("../css/common.css");

	.home {
		width: 100%;
		height: 100%;
	}

	.left-box {
		height: 100%;
		background: url(../img/login.jpg) repeat;
	}

	.right-box {
		height: 100%;
		text-align: center;
	}

	.content-title {
		color: #2CA9BB;
	}
</style>
